package com.atldy.commerce.pojo;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.atldy.commerce.commence.goods.GoodsInfo;
import com.atldy.commerce.commence.goods.GoodsProperty;
import com.atldy.commerce.commence.goods.SimpleGoodsInfo;
import com.atldy.commerce.enums.BrandCategory;
import com.atldy.commerce.enums.GoodsCategory;
import com.atldy.commerce.enums.GoodsStatus;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;

/**
 * 商品表
 *
 * @TableName t_ecommerce_goods
 */
@TableName(value = "t_ecommerce_goods")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Goods implements Serializable {
    /**
     * 自增主键
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 商品类别
     */
    private GoodsCategory goodsCategory;

    /**
     * 品牌分类
     */
    private BrandCategory brandCategory;

    /**
     * 商品名称
     */
    private String goodsName;

    /**
     * 商品图片
     */
    private String goodsPic;

    /**
     * 商品描述信息
     */
    private String goodsDescription;

    /**
     * 商品状态
     */
    private GoodsStatus goodsStatus;

    /**
     * 商品价格
     */
    private Integer price;

    /**
     * 总供应量
     */
    private Long supply;

    /**
     * 库存
     */
    private Long inventory;

    /**
     * 商品属性
     */
    private String goodsProperty;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.UPDATE)
    private Date updateTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 类型转换——GoodsInfo:Goods
     */
    public static Goods toGoods(GoodsInfo goodsInfo) {
        Goods goods = new Goods();
        goods.setGoodsCategory(GoodsCategory.of(goodsInfo.getGoodsCategory()));
        goods.setBrandCategory(BrandCategory.of(goodsInfo.getBrandCategory()));
        goods.setGoodsName(goodsInfo.getGoodsName());
        goods.setGoodsPic(goodsInfo.getGoodsPic());
        goods.setGoodsDescription(goodsInfo.getGoodsDescription());
        goods.setGoodsStatus(GoodsStatus.ONLINE);
        goods.setPrice(goodsInfo.getPrice());
        goods.setSupply(goodsInfo.getSupply());
        goods.setInventory(goodsInfo.getSupply());
        goods.setGoodsProperty(
                JSONUtil.toJsonStr(goodsInfo.getGoodsProperty())
        );
        goods.setCreateTime(goodsInfo.getCreateTime());
        goods.setUpdateTime(goodsInfo.getUpdateTime());
        return goods;
    }

    /**
     * 类型转换——Goods:GoodsInfo
     */
    public GoodsInfo toGoodsInfo() {
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(this.id);
        goodsInfo.setGoodsCategory(this.goodsCategory.getCode());
        goodsInfo.setBrandCategory(this.brandCategory.getCode());
        goodsInfo.setGoodsName(this.goodsName);
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setGoodsDescription(this.goodsDescription);
        goodsInfo.setGoodsStatus(this.goodsStatus.getStatus());
        goodsInfo.setPrice(this.price);
        goodsInfo.setGoodsProperty(
                JSON.parseObject(this.getGoodsProperty(), GoodsProperty.class)
        );
        goodsInfo.setSupply(this.supply);
        goodsInfo.setInventory(this.inventory);
        goodsInfo.setCreateTime(this.createTime);
        goodsInfo.setUpdateTime(this.updateTime);
        return goodsInfo;
    }

    /**
     * <h2>将实体对象转成 SimpleGoodsInfo 对象</h2>
     */
    public SimpleGoodsInfo toSimple() {
        SimpleGoodsInfo goodsInfo = new SimpleGoodsInfo();
        goodsInfo.setId(this.id);
        goodsInfo.setGoodsName(this.goodsName);
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setPrice(this.price);
        return goodsInfo;
    }
}